package Lab06.Extra4;

import java.util.ArrayList;
import java.util.List;

public class Executor {
    private List<ICommand> commands = new ArrayList<ICommand>();
    private List<Bar> bars = new ArrayList<Bar>();
    private List<Engineer> engineers = new ArrayList<Engineer>();
    double amount;

    public Executor() {
    }

    void add(ICommand command) {
        commands.add(command);
        if (!(command instanceof SpendMoney)) {
            if (!bars.contains(command.getBar())) {
                bars.add(command.getBar());
            }
        }else{
            if (!engineers.contains(((SpendMoney) command).getEngineer())) {
                engineers.add(((SpendMoney) command).getEngineer());
            }
        }
    }

    void run() {
        amount=0;
        commands.forEach(command -> {//第一遍遍历统计总消费数
            if (!(command instanceof SpendMoney))
                command.execute();
        });
        bars.forEach(bar -> {
            amount += bar.GetMoney();
        });
        commands.forEach(command -> {
            if (command instanceof SpendMoney) {//消费类
                ((SpendMoney) command).SetMoney(amount/engineers.size());
                command.execute();
            }

        });
        if(commands.isEmpty()){
            System.out.println("太马虎了，用错executor 还是 没消费！！！");
        }
        else if(engineers.isEmpty()){
            System.out.println("哼！想逃单？吃我闪电旋风劈！");
        }
        System.out.println();
        bars.forEach(Bar::clear);//清空账单
        commands.clear();
        bars.clear();
        engineers.clear();
    }
}
